import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ulearning_app/common/widgets/app_bar.dart';
import 'package:ulearning_app/common/widgets/app_text_fields.dart';
import 'package:ulearning_app/common/widgets/button_widgets.dart';
import 'package:ulearning_app/common/widgets/text_widgets.dart';
import 'package:ulearning_app/pages/sign_in/notifier/sign_in_notifier.dart';
import 'package:ulearning_app/pages/sign_in/sign_in_controller.dart';
import 'package:ulearning_app/pages/sign_in/widgets/sign_in_widget.dart';

//ConsumerStatefulWidget 消费组件
class SignIn extends ConsumerStatefulWidget {
  const SignIn({super.key});

  @override
  ConsumerState<SignIn> createState() => _SignInState();
}

class _SignInState extends ConsumerState<SignIn> {
  late SignInController _controller;
  @override
  void initState() {
    _controller = SignInController(ref);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final signInProvider =
        ref.watch(SignInNotifierProvider); // 注入SignInNotifierProvider
    return SafeArea(
        child: Scaffold(
            appBar: buildAppBar(title: '登录'),
            backgroundColor: Colors.white,
            body: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  //它调用了一个方法来构建并返回一个用于第三方登录的小部件 Method invocation is not a constant expression
                  thirdPartyLogin(),
                  Center(child: text14Normal(text: "或者用电子邮件登录")),
                  SizedBox(height: 20.h),
                  appTextField(
                      text: "账号",
                      iconName: "assets/icons/user.png",
                      hintText: '请输入账号',
                      obscureText: false,
                      func: (value) => ref
                          .read(SignInNotifierProvider.notifier)
                          .onEmailChange(
                              value)), // 调用SignInNotifierProvider的onEmailChange方法
                  SizedBox(height: 20.h),
                  appTextField(
                    text: "密码",
                    hintText: '请输入密码',
                    iconName: 'assets/icons/lock.png',
                    obscureText: true,
                    func: (value) => ref
                        .read(SignInNotifierProvider.notifier)
                        .onPasswordChange(
                            value), // 调用SignInNotifierProvider的onPasswordChange方法
                  ),
                  SizedBox(height: 20.h),
                  // 忘记密码
                  Container(
                    margin: EdgeInsets.only(left: 25.w),
                    child: textUnderLine(text: "忘记密码？"),
                  ),
                  SizedBox(height: 80.h),
                  // 登录按钮
                  Center(
                    child: appBtn(text: '登录', func: _controller.handleSignIn),
                  ),
                  SizedBox(height: 20.h),
                  // 注册按钮
                  Center(
                    child: appBtn(
                      text: '注册',
                      isLogin: false,
                      func: () =>
                          Navigator.pushNamed(context, '/sign_up'), // 注册页面
                    ),
                  )
                ],
              ),
            )));
  }
}
